﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Ws.Core;
using Portal.Db.Service;
using Portal.Core;
using System.Data;
using Portal.Utilities;
using Portal.AssetTn.Model;


namespace Portal.Ws.AssetTn.Bussiness
{
    /// <summary>
    /// Ham xu ly cac tu dien. Neu thich thi moi tu dien cho 1 cai nay cung duoc hoac gop chung vao
    /// </summary>
    public class AssetTnProcessCommand : ProcessCommandTemplate
    {
        public override string ServiceId
        {
            get
            {
                return "AssetTnProcessCommand";
            }
        }

        public override string ServiceName
        {
            get
            {
                return "Xử lý các chức năng liên quan đến hiện vật";
            }
        }


        public void Search(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string OrderBy = InParam.OrderBy;
            string SortBy = InParam.ExtraParam1;
            string SortType = InParam.ExtraParam2;
            if (!string.IsNullOrEmpty(SortBy))
            {
                OrderBy = SortBy;
            }
            if (string.IsNullOrEmpty(OrderBy)) OrderBy = " Subject ";
            string AssetTypeId = InParam.KeyId;
            ParamCls[] Params = ParamParser.ParseMultiRecordFromXml(InParam.XmlParams);
            string Query =
                " select ROW_NUMBER() OVER(ORDER BY " + OrderBy + ") AS RowIndex, TableAssetList.*,Icon,AssetStatusName,AssetTypeName,AssetNationalName from TableAssetList " +
                                 "  left outer join TableAssetStatus on TableAssetStatus.AssetStatusId=frkAssetStatusId" +
                                 "  left outer join TableAssetType on TableAssetType.AssetTypeId=frkAssetTypeId" +
                                 "  left outer join TableAssetNational on TableAssetNational.AssetNationalId = frkAssetNationalId" +
                                 " where 1=1 ";
            if (!string.IsNullOrEmpty(AssetTypeId))
            {
                Query += " and frkAssetTypeId='" + AssetTypeId + "' ";
            }
            bool First = true;
            bool Found = false;
            for (int iIndex = 0; iIndex < Params.Length; iIndex++)
            {
                if (!string.IsNullOrEmpty(Params[iIndex].Value))
                {
                    Found = true;
                    if (First)
                    {
                        Query += " AND (";
                    }
                    if (Params[iIndex].Like)
                    {
                        if (!First)
                        {
                            Query += " or ";
                        }
                        if (Params[iIndex].LikeLast)
                        {
                            Query += " [" + Params[iIndex].Key + "] like N'" + FuncUtilities.CorrectForSearch(Params[iIndex].Value) + "%'";
                        }
                        else
                        {
                            Query += " [" + Params[iIndex].Key + "] like N'%" + FuncUtilities.CorrectForSearch(Params[iIndex].Value) + "%'";
                        }
                    }
                    else
                    {
                        if (!First)
                        {
                            Query += " or ";
                        }
                        Query += " [" + Params[iIndex].Key + "] = N'" + FuncUtilities.CorrectForSearch(Params[iIndex].Value) + "'";
                    }
                    First = false;
                }
            }
            if (Found)
            {
                Query += ")";
            }
            Query += " order by " + OrderBy + " " + SortType;


            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, Query, null);
            string Xml = AssetParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }

        public AssetModel CreateAssetModel(WsInParam InParam, string AssetId)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string CommandText = " select TableAssetList.*,Icon,AssetStatusName,AssetTypeName,AssetNationalName,AssetMaterialName from TableAssetList " +
                                 "  left outer join TableAssetStatus on TableAssetStatus.AssetStatusId=frkAssetStatusId" +
                                 "  left outer join TableAssetNational on TableAssetNational.AssetNationalId=frkAssetNationalId" +
                                 "  left outer join TableAssetMaterial on TableAssetMaterial.AssetMaterialId=frkAssetMaterialId" +
                                 "  left outer join TableAssetType on TableAssetType.AssetTypeId=frkAssetTypeId" +
                                 " where left(AssetListId,8)=" + SpecialChar(InParam) + "AssetId  or AssetListId=" + SpecialChar(InParam) + "AssetId";


            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetId", AssetId) });
            AssetModel OAssetModel= AssetParser.ParseFromDataRow(dt.Rows[0]);
            return OAssetModel;
        }

        public void CreateAssetModel(WsInParam InParam, ref WsOutParam OutParam)
        {
            AssetModel OAssetModel = CreateAssetModel(InParam,InParam.KeyId);
            OutParam.XmlData = AssetParser.GetXml(OAssetModel);
            OutParam.HasProcessCommand = true;
        }

        public void CreateAssetTypeModel(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string AssetTypeId = InParam.KeyId;
            string CommandText = " select * from TableAssetType where AssetTypeId="+SpecialChar(InParam)+"AssetTypeId";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetTypeId", AssetTypeId) });
            string Xml = AssetTypeParser.GetXml(dt.Rows[0]);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }


        AssetMaterialCls CreateAssetMaterialModel(WsInParam InParam, string AssetMaterialId)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string CommandText = " select * from TableAssetMaterial where AssetMaterialId=" + SpecialChar(InParam) + "AssetMaterialId";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetMaterialId", AssetMaterialId) });
            AssetMaterialCls OAssetMaterial = AssetMaterialParser.ParseFromDataRow(dt.Rows[0]);
            return OAssetMaterial;
        }

        public void CreateAssetMaterialModel(WsInParam InParam, ref WsOutParam OutParam)
        {
            string AssetMaterialId = InParam.KeyId;
            AssetMaterialCls OAssetMaterialCls = CreateAssetMaterialModel(InParam, AssetMaterialId);
            string Xml = AssetMaterialParser.GetXml(OAssetMaterialCls);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }

        public void AddAsset(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string XmlData = InParam.XmlData;
            
            AssetModel OAssetModel = AssetParser.ParseFromXml(XmlData);
            string AssetListId = OAssetModel.AssetListId;
            if (string.IsNullOrEmpty(AssetListId)) AssetListId = System.Guid.NewGuid().ToString();

            if (string.IsNullOrEmpty(OAssetModel.frkAssetTypeId))
            {
                throw new Exception("Chưa xác định loại hiện vật");
            }
            if (string.IsNullOrEmpty(OAssetModel.frkAssetStatusId)) OAssetModel.frkAssetStatusId = null;
            if (string.IsNullOrEmpty(OAssetModel.frkAssetNationalId)) OAssetModel.frkAssetNationalId = null;
            if (string.IsNullOrEmpty(OAssetModel.frkAssetMaterialId)) OAssetModel.frkAssetMaterialId = null;

            CreateDBService(InParam, ConnectionString).Insert(
                null,
                "TableAssetList",
                new DbParam[] 
                { 
                    new DbParam("AssetListId", AssetListId),
                    new DbParam("Subject", OAssetModel.Subject),
                    new DbParam("Description", OAssetModel.Description),

                    new DbParam("frkAssetTypeId", OAssetModel.frkAssetTypeId),
                    new DbParam("AssetType", OAssetModel.AssetType),
                    
                    new DbParam("frkAssetStatusId", OAssetModel.frkAssetStatusId),
                    new DbParam("AssetStatus", OAssetModel.AssetStatus),

                    new DbParam("frkAssetMaterialId", OAssetModel.frkAssetMaterialId),
                    new DbParam("Material", OAssetModel.Material),

                    new DbParam("frkAssetNationalId", OAssetModel.frkAssetNationalId),
                    new DbParam("NationalName", OAssetModel.NationalName),

                    new DbParam("EntryDate", System.DateTime.Now,DbType.DateTime),
                    new DbParam("EntryBy", InParam.LoginName),
                });
            OutParam.RetKeyId = AssetListId;
            OutParam.HasProcessCommand = true;
        }

        AssetStatusCls CreateAssetStatusModel(WsInParam InParam, string AssetStatusId)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string CommandText = " select * from TableAssetStatus where AssetStatusId=" + SpecialChar(InParam) + "AssetStatusId";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetStatusId", AssetStatusId) });
            //string Xml = AssetStatusParser.GetXml(dt.Rows[0]);
            AssetStatusCls OAssetStatus = AssetStatusParser.ParseFromDataRow(dt.Rows[0]);
            return OAssetStatus;
        }

        public void CreateAssetStatusModel(WsInParam InParam, ref WsOutParam OutParam)
        {
            AssetStatusCls OAssetStatus = CreateAssetStatusModel(InParam, InParam.KeyId);
            string Xml = AssetStatusParser.GetXml(OAssetStatus);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }

        public void AddAssetStatus(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            AssetStatusCls OAssetStatusCls = AssetStatusParser.ParseFromXml(InParam.XmlData);
            if (string.IsNullOrEmpty(OAssetStatusCls.AssetStatusId))
            {
                OAssetStatusCls.AssetStatusId = System.Guid.NewGuid().ToString();
            }
            CreateDBService(InParam, ConnectionString).Insert(null,"TableAssetStatus", 
                new DbParam[] 
                { 
                    new DbParam("AssetStatusId", OAssetStatusCls.AssetStatusId),
                    new DbParam("AssetStatusName", OAssetStatusCls.AssetStatusName) 
                });
            OutParam.RetKeyId = OAssetStatusCls.AssetStatusId;
            OutParam.HasProcessCommand = true;
        }

        public void SaveAssetStatus(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            AssetStatusCls OAssetStatusCls = AssetStatusParser.ParseFromXml(InParam.XmlData);
            CreateDBService(InParam, ConnectionString).Update(null, "TableAssetStatus", "AssetStatusId", OAssetStatusCls.AssetStatusId,
                new DbParam[] 
                { 
                    new DbParam("AssetStatusName", OAssetStatusCls.AssetStatusName) 
                });
            OutParam.HasProcessCommand = true;
        }


        public void DeleteAssetStatus(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            CreateDBService(InParam, ConnectionString).ExecuteNonQuery(null, "Delete from TableAssetStatus where AssetStatusId="+SpecialChar(InParam)+"AssetStatusId",
                new DbParam[] 
                { 
                    new DbParam("AssetStatusId", InParam.KeyId) 
                });
            OutParam.HasProcessCommand = true;
        }

        AssetNationalCls CreateAssetNationalModel(WsInParam InParam, string AssetNationalId)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string CommandText = " select * from TableAssetNational where AssetNationalId=" + SpecialChar(InParam) + "AssetNationalId";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetNationalId", AssetNationalId) });
            AssetNationalCls OAssetNational = AssetNationalParser.ParseFromDataRow(dt.Rows[0]);
            return OAssetNational;
        }

        public void CreateAssetNationalModel(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            string AssetNationalId = InParam.KeyId;
            AssetNationalCls OAssetNational = CreateAssetNationalModel(InParam, AssetNationalId);
            string Xml = AssetNationalParser.GetXml(OAssetNational);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }

        public void ProcessSearchKeyword(WsInParam InParam, ref WsOutParam OutParam)
        {
            OutParam.HasProcessCommand = true;
        }

        public void ReadingAssetImage(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            string AssetId = InParam.KeyId;
            string CommandText =  "select AssetListImageId, ImageFile, Ext, IsAvatar from TableAssetListImage where left(frkAssetListId,8)="+SpecialChar(InParam)+"AssetId";
            DataTable dt = CreateDBService(InParam,ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetId", AssetId) });
            string Xml = AssetImageParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }




        public void ReadingAssetListRelation(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            string AssetId = InParam.KeyId;
            string CommandText = "select AssetListRelationId,frkRelAssetListId, ClassCode, Subject from TableAssetListRelation join TableAssetList on TableAssetList.AssetListId=frkRelAssetListId where left(frkMainAssetListId,8)=" + SpecialChar(InParam) + "AssetId";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetId", AssetId) });
            string Xml = AssetListRelationParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }

        string ConvertSortField(string field)
        {
            string[] Items = field.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            if (Items.Length == 1)
            {
                field = field + ".0";
                Items = field.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            }
            string Data = "";
            for (int iIndex = 0; iIndex < Items.Length; iIndex++)
            {
                if (iIndex > 0)
                {
                    Data = Data + ".";
                }
                Data = Data + Items[iIndex].PadLeft(5, '0');
            }
            for (int iIndex = Items.Length; iIndex <= 5; iIndex++)
            {
                Data = "".PadLeft(5, '0') + "." + Data;
            }
            return Data;
        }

        public void SaveAsset(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            AssetModel oAssetModel = AssetParser.ParseFromXml(InParam.XmlData);
            if (string.IsNullOrEmpty(oAssetModel.Subject)) throw new Exception("Tên hiện vật chưa được nhập!");
            if (!FuncUtilities.checkInteger(oAssetModel.Quantity))
            {
                oAssetModel.Quantity = "1";
            }
            if (!FuncUtilities.checkDecimal(oAssetModel.Cost))
            {
                oAssetModel.Cost = "0";
            }

            object dMadeDate = null;
            if (!string.IsNullOrEmpty(oAssetModel.MadeDate))
            {
                dMadeDate = DateTime.Parse(oAssetModel.MadeDate);
            }
            object dCollectDate = null;
            if (!string.IsNullOrEmpty(oAssetModel.CollectDate))
            {
                dCollectDate = DateTime.Parse(oAssetModel.CollectDate);
            }
            object dRegDate = null;
            if (!string.IsNullOrEmpty(oAssetModel.RegDate))
            {
                dRegDate = DateTime.Parse(oAssetModel.RegDate);
            }
            object dEntryDate = null;
            if (!string.IsNullOrEmpty(oAssetModel.EntryDate))
            {
                dEntryDate = DateTime.Parse(oAssetModel.EntryDate);
            }
            //doan nay update cac reference
            if (!string.IsNullOrEmpty(oAssetModel.frkAssetStatusId))
            {
                AssetStatusCls OAssetStatusCls = CreateAssetStatusModel(InParam, oAssetModel.frkAssetStatusId);
                oAssetModel.AssetStatus = OAssetStatusCls.AssetStatusName;
                oAssetModel.AssetStatusName = OAssetStatusCls.AssetStatusName;
            }
            else
            {
                oAssetModel.frkAssetStatusId = null;
                oAssetModel.AssetStatus = "";
                oAssetModel.AssetStatusName = "";
            }

            if (!string.IsNullOrEmpty(oAssetModel.frkAssetMaterialId))
            {
                AssetMaterialCls OAssetMaterialCls = CreateAssetMaterialModel(InParam, oAssetModel.frkAssetMaterialId);
                oAssetModel.AssetMaterialName = OAssetMaterialCls.AssetMaterialName;
                oAssetModel.Material = OAssetMaterialCls.AssetMaterialName;
            }
            else
            {
                oAssetModel.frkAssetMaterialId = null;
                oAssetModel.AssetMaterialName = "";
                oAssetModel.Material = "";
            }

            if (!string.IsNullOrEmpty(oAssetModel.frkAssetNationalId))
            {
                AssetNationalCls OAssetNationalCls = CreateAssetNationalModel(InParam, oAssetModel.frkAssetNationalId);
                oAssetModel.AssetNationalName = OAssetNationalCls.AssetNationalName;
                oAssetModel.NationalName = OAssetNationalCls.AssetNationalName;
            }
            else
            {
                oAssetModel.frkAssetNationalId = null;
                oAssetModel.AssetNationalName = "";
                oAssetModel.NationalName = "";
            }

            string FicDescription = oAssetModel.FicDescription;
            object dInputDate = null;
            if (!string.IsNullOrEmpty(oAssetModel.InputDate))
            {
                dInputDate = DateTime.Parse(oAssetModel.InputDate);
            }
            string SortInventoryNumber=ConvertSortField(oAssetModel.InventoryNumber);
            CreateDBService(InParam, ConnectionString).Update(null, "TableAssetList","AssetListId",oAssetModel.AssetListId,
                new DbParam[]{
                    new DbParam("ClassCode",oAssetModel.ClassCode),
                    new DbParam("ProfileCode",oAssetModel.ProfileCode),
                    new DbParam("InventoryCode",oAssetModel.InventoryCode),
                    new DbParam("Characteristic",oAssetModel.Characteristic),
                    new DbParam("Subject",oAssetModel.Subject),
                    new DbParam("Quantity",oAssetModel.Quantity),
                    new DbParam("Size",oAssetModel.Size),
                    new DbParam("MadeBy",oAssetModel.MadeBy),
                    new DbParam("MadePlace",oAssetModel.MadePlace),
                    new DbParam("StoragePlace",oAssetModel.StoragePlace),
                    new DbParam("Note",oAssetModel.Note),
                    
                    new DbParam("frkAssetMaterialId",oAssetModel.frkAssetMaterialId),
                    new DbParam("Material",oAssetModel.Material),

                    new DbParam("DeliverBy",oAssetModel.DeliverBy),
                    new DbParam("UseFor",oAssetModel.UseFor),
                    new DbParam("Cost",oAssetModel.Cost),
                    new DbParam("TechnicalMade",oAssetModel.TechnicalMade),
                    new DbParam("ContentHistory",oAssetModel.ContentHistory),
                    new DbParam("Description",oAssetModel.Description),
                    new DbParam("OtherSubject",oAssetModel.OtherSubject),
                    new DbParam("Report",oAssetModel.Report),
                    
                    new DbParam("AssetStatus",oAssetModel.AssetStatus),
                    new DbParam("frkAssetStatusId",oAssetModel.frkAssetStatusId),
                    
                    new DbParam("NationalName",oAssetModel.NationalName),
                    new DbParam("frkAssetNationalId",oAssetModel.frkAssetNationalId),
                    
                    new DbParam("InventoryNumber",oAssetModel.InventoryNumber),
                    new DbParam("ReceiveBy",oAssetModel.ReceiveBy),
                    new DbParam("CollectCode",oAssetModel.CollectCode),
                    new DbParam("CollectDate",dCollectDate,DbType.DateTime),
                    new DbParam("RegDate",dRegDate,DbType.DateTime),
                    new DbParam("MadeDate",dMadeDate,DbType.DateTime),
                    new DbParam("SortInventoryNumber",SortInventoryNumber),
                    new DbParam("FicDescription",FicDescription),
                    new DbParam("InputDate",dMadeDate,DbType.DateTime),
                });
            OutParam.HasProcessCommand = true;
        }

        public void DeleteAsset(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            string AssetListId = InParam.KeyId;
            string CommandText =
                " Delete from TableAssetListMadeBy where left(frkAssetListId,8)="+SpecialChar(InParam)+"AssetListId; "+
                " Delete from TableAssetListRelation where left(frkMainAssetListId,8)="+SpecialChar(InParam)+"AssetListId or left(frkRelAssetListId,8)="+SpecialChar(InParam)+"AssetListId " +
                " Delete from TableAssetListImage where left(frkAssetListId,8)="+SpecialChar(InParam)+"AssetListId; " +
                " Delete from TableAssetListHistory where left(frkAssetListId,8)="+SpecialChar(InParam)+"AssetListId; " +
                " Delete from TableAssetList where left(AssetListId,8)="+SpecialChar(InParam)+"AssetListId";
      
            CreateDBService(InParam, ConnectionString).ExecuteNonQuery(null, CommandText, new DbParam[] { new DbParam("AssetListId", AssetListId) });
            OutParam.HasProcessCommand = true;
        }

        public void ReadingAssetHistory(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            string AssetId = InParam.KeyId;
            string CommandText = "select * from TableAssetListHistory where left(frkAssetListId,8)=" + SpecialChar(InParam) + "AssetId";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, new DbParam[] { new DbParam("AssetId", AssetId) });
            string Xml = AssetHistoryParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }

        public void ReadingAssetType(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            string AssetId = InParam.KeyId;
            string CommandText = "select * from TableAssetType order by AssetTypeId";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, null);
            string Xml = AssetTypeParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }


        public void ReadingAssetStatus(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);

            string AssetId = InParam.KeyId;
            string CommandText = "select * from TableAssetStatus where 1=1";
            if (!string.IsNullOrEmpty(InParam.Keyword))
            {
                CommandText += " and AssetStatusName like N'%"+InParam.Keyword+"%'";
            }
            CommandText+=" order by AssetStatusName";
            
            
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, null);
            string Xml = AssetStatusParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }


        public void ReadingAssetMaterial(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);

            string AssetId = InParam.KeyId;
            string CommandText = "select * from TableAssetMaterial where 1=1";
            if (!string.IsNullOrEmpty(InParam.Keyword))
            {
                CommandText += " and AssetMaterialName like N'%" + InParam.Keyword + "%'";
            }
            CommandText += " order by AssetMaterialName";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, null);
            string Xml = AssetMaterialParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }



        public void AddAssetMaterial(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            AssetMaterialCls OAssetMaterialCls = AssetMaterialParser.ParseFromXml(InParam.XmlData);
            if (string.IsNullOrEmpty(OAssetMaterialCls.AssetMaterialId))
            {
                OAssetMaterialCls.AssetMaterialId = System.Guid.NewGuid().ToString();
            }
            CreateDBService(InParam, ConnectionString).Insert(null, "TableAssetMaterial",
                new DbParam[] 
                { 
                    new DbParam("AssetMaterialId", OAssetMaterialCls.AssetMaterialId),
                    new DbParam("AssetMaterialName", OAssetMaterialCls.AssetMaterialName) 
                });
            OutParam.RetKeyId = OAssetMaterialCls.AssetMaterialId;
            OutParam.HasProcessCommand = true;
        }

        public void SaveAssetMaterial(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            AssetMaterialCls OAssetMaterialCls = AssetMaterialParser.ParseFromXml(InParam.XmlData);
            CreateDBService(InParam, ConnectionString).Update(null, "TableAssetMaterial", "AssetMaterialId", OAssetMaterialCls.AssetMaterialId,
                new DbParam[] 
                { 
                    new DbParam("AssetMaterialName", OAssetMaterialCls.AssetMaterialName) 
                });
            OutParam.HasProcessCommand = true;
        }

        public void DeleteAssetMaterial(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            CreateDBService(InParam, ConnectionString).ExecuteNonQuery(null, "Delete from TableAssetMaterial where AssetMaterialId=" + SpecialChar(InParam) + "AssetMaterialId",
                new DbParam[] 
                { 
                    new DbParam("AssetMaterialId", InParam.KeyId) 
                });
            OutParam.HasProcessCommand = true;
        }

        public void ReadingAssetNational(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);

            string AssetId = InParam.KeyId;
            string CommandText = "select * from TableAssetNational where 1=1";
            if (!string.IsNullOrEmpty(InParam.Keyword))
            {
                CommandText += " and AssetNationalName like N'%" + InParam.Keyword + "%'";
            }
            CommandText += " order by AssetNationalName";
            DataTable dt = CreateDBService(InParam, ConnectionString).GetDataTable(null, CommandText, null);
            string Xml = AssetNationalParser.GetXml(dt);
            OutParam.XmlData = Xml;
            OutParam.HasProcessCommand = true;
        }




        public void AddAssetNational(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            AssetNationalCls OAssetNationalCls = AssetNationalParser.ParseFromXml(InParam.XmlData);
            if (string.IsNullOrEmpty(OAssetNationalCls.AssetNationalId))
            {
                OAssetNationalCls.AssetNationalId = System.Guid.NewGuid().ToString();
            }
            CreateDBService(InParam, ConnectionString).Insert(null, "TableAssetNational",
                new DbParam[] 
                { 
                    new DbParam("AssetNationalId", OAssetNationalCls.AssetNationalId),
                    new DbParam("AssetNationalName", OAssetNationalCls.AssetNationalName) 
                });
            OutParam.RetKeyId = OAssetNationalCls.AssetNationalId;
            OutParam.HasProcessCommand = true;
        }

        public void SaveAssetNational(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            AssetNationalCls OAssetNationalCls = AssetNationalParser.ParseFromXml(InParam.XmlData);
            CreateDBService(InParam, ConnectionString).Update(null, "TableAssetNational", "AssetNationalId", OAssetNationalCls.AssetNationalId,
                new DbParam[] 
                { 
                    new DbParam("AssetNationalName", OAssetNationalCls.AssetNationalName) 
                });
            OutParam.HasProcessCommand = true;
        }

        public void DeleteAssetNational(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            CreateDBService(InParam, ConnectionString).ExecuteNonQuery(null, "Delete from TableAssetNational where AssetNationalId=" + SpecialChar(InParam) + "AssetNationalId",
                new DbParam[] 
                { 
                    new DbParam("AssetNationalId", InParam.KeyId) 
                });
            OutParam.HasProcessCommand = true;
        }

        public void SaveAssetTypeFieldConfig(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            string AssetTypeId = InParam.KeyId;
            string XmlFieldConfig = InParam.XmlData;
            string CommandText = " Update TableAssetType set XmlFieldConfig=" + SpecialChar(InParam) + "XmlFieldConfig where AssetTypeId=" + SpecialChar(InParam) + "AssetTypeId";
            CreateDBService(InParam, ConnectionString).ExecuteNonQuery(null, CommandText, new DbParam[]{
                new DbParam("AssetTypeId",AssetTypeId),
                new DbParam("XmlFieldConfig",XmlFieldConfig),
            });
            
            OutParam.HasProcessCommand = true;
        }

        public void SaveAssetTypeSearchFieldConfig(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            
            string AssetTypeId = InParam.KeyId;
            string XmlConfigSearch = InParam.XmlData;
            string CommandText = " Update TableAssetType set XmlConfigSearch=" + SpecialChar(InParam) + "XmlConfigSearch where AssetTypeId=" + SpecialChar(InParam) + "AssetTypeId";
            CreateDBService(InParam, ConnectionString).ExecuteNonQuery(null, CommandText, new DbParam[]{
                new DbParam("AssetTypeId",AssetTypeId),
                new DbParam("XmlConfigSearch",XmlConfigSearch),
            });
            
            OutParam.HasProcessCommand = true;
        }


        public void SaveAssetTypePrintFieldConfig(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);

            string AssetTypeId = InParam.KeyId;
            string XmlConfigSearch = InParam.XmlData;
            string CommandText = " Update TableAssetType set XmlConfigPrint=" + SpecialChar(InParam) + "XmlConfigPrint where AssetTypeId=" + SpecialChar(InParam) + "AssetTypeId";
            CreateDBService(InParam, ConnectionString).ExecuteNonQuery(null, CommandText, new DbParam[]{
                new DbParam("AssetTypeId",AssetTypeId),
                new DbParam("XmlConfigPrint",XmlConfigSearch),
            });

            OutParam.HasProcessCommand = true;
        }

        public void AddAssetImages(WsInParam InParam, ref WsOutParam OutParam)
        {
            if (string.IsNullOrEmpty(InParam.ExtraParam1)) throw new Exception("Không tìm thấy dữ liệu ảnh minh họa upload!");
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            SiteParam oSiteParam = new SiteParam();
            oSiteParam.Parse(InParam.SiteXml);
            string Pathsave = oSiteParam.MediaPathRoot + "\\asset.images";
            System.IO.Directory.CreateDirectory(Pathsave);

            string AssetListId = InParam.KeyId;
            AssetModel OAssetModel = CreateAssetModel(InParam, AssetListId);
            AssetListId = OAssetModel.AssetListId;
            string CommandText = " Insert into [TableAssetListImage](AssetListImageId, frkAssetListId, ImageFile, [Content], Ext, IsAvatar) values (" + SpecialChar(InParam) + "AssetListImageId, " + SpecialChar(InParam) + "frkAssetListId, " + SpecialChar(InParam) + "ImageFile, " + SpecialChar(InParam) + "Content, " + SpecialChar(InParam) + "Ext, " + SpecialChar(InParam) + "IsAvatar)";
            string[] Files = InParam.ExtraParam1.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);            
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                string AssetListImageId = System.Guid.NewGuid().ToString();
                System.IO.FileInfo fi = new System.IO.FileInfo(Files[iIndex]);
                CreateDBService(InParam, ConnectionString).ExecuteNonQuery(
                    null,
                    CommandText,
                     new DbParam[]
                    {
                          new DbParam("AssetListImageId",AssetListImageId),
                          new DbParam("frkAssetListId",AssetListId),
                          new DbParam("ImageFile",fi.Name),
                          new DbParam("Content", FuncUtilities.GetBytesFromFile(fi.FullName),DbType.Binary),
                          new DbParam("Ext",fi.Extension),
                          new DbParam("IsAvatar",0),
                    });
                string DestFile = Pathsave + "\\" + AssetListImageId + fi.Extension;
                System.IO.File.Copy(fi.FullName, DestFile, true);
            }
            OutParam.HasProcessCommand = true;
        }

        public void DeleteAssetImage(WsInParam InParam, ref WsOutParam OutParam)
        {
            string ConnectionString = AssetTnUtilities.GetConnectionString(InParam);
            SiteParam oSiteParam = new SiteParam();
            oSiteParam.Parse(InParam.SiteXml);
            string Pathsave = oSiteParam.MediaPathRoot + "\\asset.images";
            System.IO.Directory.CreateDirectory(Pathsave);

            string AssetListImageId = InParam.KeyId;
            string CommandText =
                " Select ext from TableAssetListImage where AssetListImageId=" + SpecialChar(InParam) + "AssetListImageId; " +
                " Delete from TableAssetListImage where AssetListImageId=" + SpecialChar(InParam) + "AssetListImageId";
            DataTable dtResult = CreateDBService(InParam, ConnectionString).GetDataTable(
                null,
                CommandText,
                    new DbParam[]
                {
                        new DbParam("AssetListImageId",AssetListImageId),
                });
            
            string ext = (string)dtResult.Rows[0]["ext"];
            string DestFile = Pathsave + "\\" + AssetListImageId + ext;
            try
            {
                System.IO.File.Delete(DestFile);
            }
            catch { }
        
            OutParam.HasProcessCommand = true;
        }

        public override void ProcessCommand(WsInParam InParam, ref WsOutParam OutParam)
        {
            OutParam.ProcessServiceId = ServiceId;
            if (InParam.Command.ToLower().Equals("Search".ToLower()))
            {
                //xu ly command o day
                Search(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("CreateAssetModel".ToLower()))
            {
                //xu ly command o day
                CreateAssetModel(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("CreateAssetTypeModel".ToLower()))
            {
                //xu ly command o day
                CreateAssetTypeModel(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("ProcessSearchKeyword".ToLower()))
            {
                //xu ly command o day
                ProcessSearchKeyword(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("ReadingAssetImage".ToLower()))
            {
                //xu ly command o day
                ReadingAssetImage(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("ReadingAssetListRelation".ToLower()))
            {
                //xu ly command o day
                ReadingAssetListRelation(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("ReadingAssetHistory".ToLower()))
            {
                //xu ly command o day
                ReadingAssetHistory(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("SaveAsset".ToLower()))
            {
                //xu ly command o day
                SaveAsset(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("DeleteAsset".ToLower()))
            {
                //xu ly command o day
                DeleteAsset(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("AddAssetImages".ToLower()))
            {
                //xu ly command o day
                AddAssetImages(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("DeleteAssetImage".ToLower()))
            {
                //xu ly command o day
                DeleteAssetImage(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }


            if (InParam.Command.ToLower().Equals("ReadingAssetType".ToLower()))
            {
                //xu ly command o day
                ReadingAssetType(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }


            if (InParam.Command.ToLower().Equals("SaveAssetTypeFieldConfig".ToLower()))
            {
                //xu ly command o day
                SaveAssetTypeFieldConfig(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("SaveAssetTypeSearchFieldConfig".ToLower()))
            {
                //xu ly command o day
                SaveAssetTypeSearchFieldConfig(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            if (InParam.Command.ToLower().Equals("SaveAssetTypePrintFieldConfig".ToLower()))
            {
                //xu ly command o day
                SaveAssetTypePrintFieldConfig(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //ReadingAssetStatus
            if (InParam.Command.ToLower().Equals("ReadingAssetStatus".ToLower()))
            {
                //xu ly command o day
                ReadingAssetStatus(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //CreateAssetStatusModel
            if (InParam.Command.ToLower().Equals("CreateAssetStatusModel".ToLower()))
            {
                //xu ly command o day
                CreateAssetStatusModel(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }


            //AddAssetStatus
            if (InParam.Command.ToLower().Equals("AddAssetStatus".ToLower()))
            {
                //xu ly command o day
                AddAssetStatus(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //SaveAssetStatus
            if (InParam.Command.ToLower().Equals("SaveAssetStatus".ToLower()))
            {
                //xu ly command o day
                SaveAssetStatus(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //DeleteAssetStatus
            if (InParam.Command.ToLower().Equals("DeleteAssetStatus".ToLower()))
            {
                //xu ly command o day
                DeleteAssetStatus(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }


            //ReadingAssetNational
            if (InParam.Command.ToLower().Equals("ReadingAssetNational".ToLower()))
            {
                //xu ly command o day
                ReadingAssetNational(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //CreateAssetNationalModel
            if (InParam.Command.ToLower().Equals("CreateAssetNationalModel".ToLower()))
            {
                //xu ly command o day
                CreateAssetNationalModel(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }




            //AddAssetNational
            if (InParam.Command.ToLower().Equals("AddAssetNational".ToLower()))
            {
                //xu ly command o day
                AddAssetNational(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //SaveAssetNational
            if (InParam.Command.ToLower().Equals("SaveAssetNational".ToLower()))
            {
                //xu ly command o day
                SaveAssetNational(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //DeleteAssetNational
            if (InParam.Command.ToLower().Equals("DeleteAssetNational".ToLower()))
            {
                //xu ly command o day
                DeleteAssetNational(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }



            //ReadingAssetMaterial
            if (InParam.Command.ToLower().Equals("ReadingAssetMaterial".ToLower()))
            {
                //xu ly command o day
                ReadingAssetMaterial(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //CreateAssetMaterialModel
            if (InParam.Command.ToLower().Equals("CreateAssetMaterialModel".ToLower()))
            {
                //xu ly command o day
                CreateAssetMaterialModel(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }


            //AddAssetMaterial
            if (InParam.Command.ToLower().Equals("AddAssetMaterial".ToLower()))
            {
                //xu ly command o day
                AddAssetMaterial(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //SaveAssetMaterial
            if (InParam.Command.ToLower().Equals("SaveAssetMaterial".ToLower()))
            {
                //xu ly command o day
                SaveAssetMaterial(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //DeleteAssetMaterial
            if (InParam.Command.ToLower().Equals("DeleteAssetMaterial".ToLower()))
            {
                //xu ly command o day
                DeleteAssetMaterial(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }

            //AddAsset
            if (InParam.Command.ToLower().Equals("AddAsset".ToLower()))
            {
                //xu ly command o day
                AddAsset(InParam, ref OutParam);
                if (OutParam.HasProcessCommand) return;//neu da xu ly thi bien luon...!
            }
        }
    }
}
